package com.woniuxy.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.core.mapper.ShopMapper;
import com.woniuxy.core.service.ShopService;
import com.woniuxy.entity.AllShop;
import com.woniuxy.entity.DTO.PageResponseDTO;
import com.woniuxy.entity.DTO.ShopAndUserDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author hq
 * @Date 2025/7/13$ 15:55$
 * @Description TODO
 * @Params $
 * @Return $
 **/
@Service
public class ShopServiceImpl implements ShopService {
    @Autowired
    private ShopMapper shopMapper;
    /**
     * 创建商品
     * @param shop
     */
    @Override
    public void createShop(AllShop shop) {
        //初始化创建时间和修改时间
        shop.setCreateTime(new java.util.Date());
        shop.setUpdateTime(new java.util.Date());
        shopMapper.insert(shop);
    }

    /**
     * 使用mybatisplus进行分页查询
     * @return
     */
    @Override
    public PageResponseDTO<ShopAndUserDTO> getAllShops(Integer pageNum, Integer pageSize, String title, Integer typeId) {
        // 创建分页对象，并启用 count 查询
        Page<ShopAndUserDTO> page = new Page<>(pageNum, pageSize, true);
        // 执行分页查询
        Page<ShopAndUserDTO> resultPage = shopMapper.selectShopsWithUserInfo(page, title, typeId);
        // 返回封装结果
        return PageResponseDTO.build(resultPage);
    }

    @Override
    public void deleteShop(Integer id) {
        shopMapper.deleteById(id);
    }

    @Override
    public void offlineShop(Integer id, Integer status) {
        //更新修改时间
        shopMapper.updateStatus(id, status);
    }

    @Override
    public AllShop getShopById(Integer id) {
        return shopMapper.getShopById(id);
    }
    @Override
    public List<AllShop> getShopByUserId(Integer userId) {
        return shopMapper.selectList(new QueryWrapper<AllShop>().eq("user_id", userId));
    }
}
